#!/usr/bin/env python3
import sys, os
import argparse
import yaml  # pip install pyyaml
import logging
logging.basicConfig(stream=sys.stderr, level=logging.INFO, format='%(asctime)s [%(levelname)s] %(message)s')

MODEL_ZOO   = os.path.abspath('models');   assert os.path.isdir(MODEL_ZOO)
DATASET_ZOO = os.path.abspath('datasets'); assert os.path.isdir(DATASET_ZOO)

if __name__ == '__main__':
    parser = argparse.ArgumentParser()

    parser.add_argument('-m', '--model',   type = str)
    parser.add_argument('-d', '--dataset', type = str)

    args = parser.parse_args()
    logging.info(args)


    if not os.path.isfile('utils/oss'):
        logging.error("Please install oss via utils/install_aliyun_oss_client.sh")
        sys.exit(-1)

    if not os.path.isfile('credentials/aliyun_oss.cfg'):
        instructions_to_get_credential = (
            'You need credential to use the leaderboard:\n  '
            'Please send email with title "oss.cfg" to leaderboard@speechio.ai, '
            'and paste replied content to credentials/aliyun_oss.cfg'
        )
        logging.error(instructions_to_get_credential)
        sys.exit(-1)


    if args.model and not args.dataset:
        model_id = args.model
        with open(os.path.join(MODEL_ZOO, 'zoo.yaml'), 'r', encoding='utf8') as f:
            remote_model_zoo = yaml.safe_load(f)
        src = remote_model_zoo[model_id]['url']
        dst = os.path.join(MODEL_ZOO, model_id)

    elif args.dataset and not args.model:
        dataset_id = args.dataset
        with open(os.path.join(DATASET_ZOO, 'zoo.yaml'), 'r', encoding='utf8') as f:
            remote_dataset_zoo = yaml.safe_load(f)
        src = remote_dataset_zoo[dataset_id]['url']
        dst = os.path.join(DATASET_ZOO, dataset_id)

    else:
        logging.info(
            F'\n  {__file__} -m <MODEL_ID>\n'
            F'\n  {__file__} -d <DATASET_ID>\n'
        )
        exit(0)

    cmd = F'utils/oss -c credentials/aliyun_oss.cfg cp -ur {src} {dst}'
    logging.info(cmd)
    os.system(cmd)
